package com.chub.study.modules.controller;

import cn.hutool.core.collection.IterUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chub.RespResult;
import com.chub.study.modules.entity.TDictInfo;
import com.chub.study.modules.service.TDictInfoService;
import com.chub.study.modules.vo.ReqDictInfoVo;
import com.chub.study.util.SmartWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * <p>
 * 字典信息表
 * </p>
 *
 * @author Chub
 * @since 2023-04-13
 */
@Api(tags = "字典信息表服务")
@Slf4j
@RestController
@RequestMapping("/modules/tdictinfo")
public class TDictInfoController {

    @Resource
    private TDictInfoService tDictInfoService;

    @ApiOperation("创建")
    @PostMapping("/create")
    public RespResult<String> create(@Valid @RequestBody TDictInfo tDictInfo) {
        tDictInfoService.save(tDictInfo);
        return RespResult.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public RespResult<String> delete(@RequestParam String id) {
        tDictInfoService.removeById(id);
        return RespResult.success();
    }

    @ApiOperation(value = "修改")
    @PostMapping("/modify")
    public RespResult<String> modify(@Valid @RequestBody TDictInfo tDictInfo) {
        tDictInfoService.updateById(tDictInfo);
        return RespResult.success();
    }

    @ApiOperation(value = "查询详情")
    @GetMapping("/detail")
    public RespResult<TDictInfo> detail(@RequestParam String id) {
        return RespResult.success(tDictInfoService.getById(id));
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/queryByPage")
    public RespResult<IPage<TDictInfo>> queryByPage(@RequestBody JSONObject queryObject) {

         SmartWrapper<TDictInfo> smartWrapper = new SmartWrapper<>();
         smartWrapper.parse(queryObject, TDictInfo.class);

         QueryWrapper<TDictInfo> queryWrapper = smartWrapper.getQueryWrapper();
         IPage<TDictInfo> page = smartWrapper.generatePage();
         tDictInfoService.page(page, queryWrapper);
         return RespResult.success(page);
    }

    @ApiOperation(value = "取字典")
    @PostMapping("/getBydictTypeCodes")
    public RespResult<Map<String, List<TDictInfo>>> getBydictTypeCodes(@RequestBody Map<String, List<String>> reqMap) {

        log.info(" dictTypeCodes:{}", JSON.toJSONString(reqMap));

        JSONObject json = new JSONObject();


        // 接收List
        List<String> strList = (List<String>) reqMap.get("dictTypeCodes");

        if (strList == null || strList.isEmpty()) {
            strList =  Stream.of("DEL_FLAG").collect(Collectors.toList());
        }

        Set<String> ids = new HashSet<>(strList);

        Map<String, List<TDictInfo>> result = tDictInfoService.getBydictTypeCodes(ids);
        return RespResult.success(result);
        // return RespResult.failed(result);
    }

    @ApiOperation(value = "取字典返回Map")
    @PostMapping("/getBydictTypeCodesReturnMap")
    // public Map<String, List<TDictInfo>> getBydictTypeCodesReturnMap(@Valid @RequestBody Map<String, List<String>> reqMap) {
    public Map<String, List<TDictInfo>> getBydictTypeCodesReturnMap(@Validated @RequestBody ReqDictInfoVo reqMap) {
        log.info(" dictTypeCodes-dictTypeCode:{};", JSON.toJSONString(reqMap));

        JSONObject json = new JSONObject();


        // 接收List
        // List<String> strList = (List<String>) reqMap.get("dictTypeCodes");
        List<String> strList = reqMap.getDictTypeCodes();

        if (strList == null || strList.isEmpty()) {
            strList =  Stream.of("DEL_FLAG").collect(Collectors.toList());
        }

        Set<String> ids = new HashSet<String>(strList);

        Map<String, List<TDictInfo>> result = tDictInfoService.getBydictTypeCodes(ids);
        return result;
    }


    /**
     * @description : 批量插入
     *
     * @param  : dictInfos
     * @return : int
     * @author : chub
     * @date   : 2023-10-16 15:44
     */
    @ApiOperation(value = "批量插入字典数据")
    @PostMapping("/batchInsert")
    public int batchInsert(@RequestBody List<TDictInfo> dictInfos) {
    // 参数格式如下：
    // [
    //     {
    //         "dictTypeCode": "DEL_TEST",
    //             "dictTypeName": "删除测试",
    //             "dictItemCode": "0",
    //             "dictItemName": "未删除"
    //     },
    //     {
    //
    //         "dictTypeCode": "DEL_TEST",
    //             "dictTypeName": "删除测试",
    //             "dictItemCode": "1",
    //             "dictItemName": "已删除"
    //     }
    // ]


        if (IterUtil.isEmpty(dictInfos)) {
            log.info("输入参数不能为空。");
        }

        log.info(" dictInfos:{}", JSON.toJSONString(dictInfos));

        // 自己写SQL
        // return tDictInfoService.batchInsert(dictInfos);

        // 这里不需要自己写SQL，也可以批量插入数据
        return tDictInfoService.insertBatchSomeColumn(dictInfos);

    }

}